package maosy.oauth2.client.test.http

import com.google.gson.Gson
import maosy.lib.http.HttpClientUtils
import maosy.lib.http.HttpUtils
import org.apache.commons.codec.digest.DigestUtils
import org.apache.http.NameValuePair
import org.apache.http.message.BasicNameValuePair
import org.junit.Test

class UserTest {
    private val baseUrl = "http://127.0.0.1:8055"
    private val testUrl = "http://192.168.2.104:7355"

    private val gson = Gson()

    @Test
    fun createUser() {
        val token = "561f6e34-cbc4-497a-9ff6-252a5137757b"
        val url = "$testUrl/api/user/create?access_token=$token"
        HttpClientUtils.httpPostBody(
            url, gson.toJson(
                mapOf(
                    "userName" to "mengyinlu",
                    "password" to DigestUtils.md5Hex("123456"),
                    "isvId" to "",
                    "userType" to 2,
                    "realName" to "孟饮露"
                )
            ), { code, body ->
                println("http status:$code")
                println("response body: $body")
            }, { code, e ->
                println("http status:$code")
                e.printStackTrace()
            }
        )
    }

    @Test
    fun getUser() {
        val url = "$baseUrl/api/user?access_token=5bc44186-fca6-41f1-a501-db334c24ff24"
        val result = HttpUtils.httpGet(url, null)
        println(result)
    }

    @Test
    fun checkTokenTest() {
        val url = "$testUrl/oauth/check_token?token=eb2abd44-a109-40cb-b49b-6c56716f4606"
        val result = HttpUtils.httpGet(url, null)
        println(result)
    }

    @Test
    fun logoutTest() {
        val url = "$baseUrl/api/logout/27affd20-0207-43c0-af6a-392bbd827027"
        val result = HttpUtils.httpGet(url, null)
        println(result)
    }

    @Test
    fun apiTest() {
        val url = "$baseUrl/api/adminUser?access_token=1166eddf-0204-47bb-8066-51494a5df322"
        val result = HttpUtils.httpGet(url, null)
        println(result)
    }

    @Test
    fun refreshTokenTest() {
        val url = "$baseUrl/oauth/token"

        val params = listOf<NameValuePair>(
            BasicNameValuePair("client_id", "demoApp"),
            BasicNameValuePair("client_secret", "demoAppSecret"),
            BasicNameValuePair("grant_type", "refresh_token"),
            BasicNameValuePair("refresh_token", "2f346744-8063-43c2-b7a4-4f8b45dfe367")
//            BasicNameValuePair("username", "demoUser1"),
//            BasicNameValuePair("password", "123456")
        )

        val result = HttpUtils.httpPost(url, params)

        println(result)
    }

}